package com.daily.daily_push.excel;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/excel")
@Api(tags = {"【Excel 导出控制层】"})
@Slf4j
public class MyExcelController {

    @GetMapping("/export")
    @ApiOperation("导出 Excel 到本地路径")
    public String export(@RequestParam String savePath) { // 接收本地路径参数，例如：D:/downloads
        try {
            List<InterviewInformationVO> exportList = new ArrayList<>();
            buildInterviewInformation(exportList);

            // 生成文件名
            String fileName = "面试记录表-" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";

            // 检查路径是否存在，不存在则创建
            File dir = new File(savePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            // 生成完整文件路径
            File outputFile = new File(dir, fileName);

            // 写入 Excel 到本地
            EasyExcel.write(outputFile, InterviewInformationVO.class)
                    .registerWriteHandler(new CustomTitleStyleHandler())
                    .registerWriteHandler(new CustomContentStyleHandler())
                    .sheet("sheet1")
                    .doWrite(exportList);


//            // 写入 Excel 到本地
//            EasyExcel.write(outputFile, InterviewInformationVO.class)
//                    .registerWriteHandler(new CustomTitleStyleHandler()) // 添加样式处理器
//                    .sheet("sheet1")
//                    .doWrite(exportList);

            return "导出成功！文件路径：" + outputFile.getAbsolutePath();
        } catch (Exception e) {
            log.error("导出失败: {}", e.getMessage());
            return "导出失败: " + e.getMessage();
        }
    }

    public void buildInterviewInformation(List<InterviewInformationVO> exportList) {
        InterviewInformationVO interviewInformationVO = new InterviewInformationVO();
        interviewInformationVO.setName("张三");
        interviewInformationVO.setAge(25);
        interviewInformationVO.setSex("男");
        interviewInformationVO.setNativePlace("湖北");
        interviewInformationVO.setSelfEvaluation("我是一个工作狂");
        interviewInformationVO.setCmmunicationAkillsScore1A(90);
        interviewInformationVO.setCmmunicationAkillsScore1B(85);
        interviewInformationVO.setProfessionalAbilityScore1A(90);
        interviewInformationVO.setProfessionalAbilityScore1B(90);
        interviewInformationVO.setCmmunicationAkillsScore1A(85);
        interviewInformationVO.setCmmunicationAkillsScore1B(80);
        interviewInformationVO.setProfessionalAbilityScore1A(75);
        interviewInformationVO.setProfessionalAbilityScore1B(80);
        exportList.add(interviewInformationVO);
    }

}
